let app = getApp()
import detailCanvas from '../../utils/sharegoodsCanvas.js';
Component({
    properties: {
      activity:String,
      goodId:Number,
      cartCount:{
        type:Number,
        observer: function(newVal, oldVal) {
          this.updatecartCount()
        }
      },
      }, 
      data:{
        interval:2000,
        endVisible:false,
        modalvisible:false,
        moneySign:'¥',
        collectionStatus: 0,
        saveText:"保存到相册，分享给好友",
        autoplay: !0,
        hideVideo: !1,
        duration: 500,
        showSales:true,
        tabCur: 0,
        detailTabList: [ {
              title: "商品详情"
            },{
              title: "商品属性"
            },{
              title: "商品评价"
            }
        ]
      }, 
     pageLifetimes: {
      show: function () {
        if(this.data.activity!=='integral'&&this.data.activity!=='group'&&this.data.activity!=='bargain'&&this.data.activity!=='seckill'&&this.data.activity!=='service'&&this.data.activity!=='integral_list'&&this.data.activity!=='group_list'&&this.data.activity!=='bargain_list'&&this.data.activity!=='seckill_list'&&this.data.activity!=='service_list'){
          this.setData({
            activity:'goods_style'
          })
        }
        this.setData({
          globalColor:app.globalData.globalColor
        })
        var that=this
        if(this.data.activity=='integral'||this.data.activity=='integral_list'){
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Integralmall_getIntegralGoods',
              id:that.data.goodId
            },
            success: function(res) {
              if(res.data.code==301){
                wx.showModal({
                  content:res.data.msg,     
                  showCancel: !1,
                  confirmText: "点击返回",
                  confirmColor: "#333",
                  success: function(e) {
                      e.confirm && wx.navigateBack();
                  }
              })
              }else{
                var productInfo=res.data.data.goods
                productInfo['banner']=JSON.parse(res.data.data.goods.pic_group)
                wx.setStorageSync('thumbimg',productInfo['banner'][0])
                that.setData({
                  activityInfo:res.data.data.activity,
                  productInfo:productInfo
                 })
              }
            }
          })
        }else if(this.data.activity=='group'||this.data.activity=='group_list'){
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Group_getGroup',
              id:that.data.goodId,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
              if(res.data.data.length==0){
                that.setData({
                  msg:res.data.msg,
                  modalvisible:true
                })
              }
              if(res.data.data.activity.end_time<=0){
                 that.setData({
                  activityEnd:true
                 })
              }
              var productInfo=res.data.data.goods
              productInfo['banner']=JSON.parse(res.data.data.goods.pic_group)
              wx.setStorageSync('thumbimg',productInfo['banner'][0])
               that.setData({
                activityInfo:res.data.data.activity,
                productInfo:productInfo
               })
            }
          })
        }else if(this.data.activity=='bargain'||this.data.activity=='bargain_list'){
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Bargain_getBargain',
              id:that.data.goodId,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {  
              var endtime=Date.parse(new Date())/1000+res.data.data.activity.end_time
              var productInfo=res.data.data.goods
              productInfo['banner']=JSON.parse(res.data.data.goods.pic_group)
              wx.setStorageSync('thumbimg',productInfo['banner'][0])
               that.setData({
                activityend_time:endtime,
                activityInfo:res.data.data.activity,
                productInfo:productInfo
               })
            }
          })
        }
        else if(this.data.activity=='service'||this.data.activity=='service_list'){
          var detailTabList=[ {
            title: "预约详情"
            }]
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Services_getServices',
              id:that.data.goodId
            },
            success: function(res) {
              var  productInfo={
                goods_name:res.data.info.name,
                description:res.data.info.content,
                pic:res.data.info.images,
                pic_group:JSON.stringify([ res.data.info.images ]),
                price:res.data.info.price,
                market_price:res.data.info.original_price,
                promote_word: res.data.info.description,
                service_status:res.data.info.is_services,
                id:res.data.info.id
              }
              productInfo['banner']=JSON.parse(productInfo.pic_group)
              wx.setStorageSync('thumbimg',productInfo['banner'][0])
              that.setData({
                productInfo:productInfo,
                activityInfo:res.data.info,
                detailTabList:detailTabList
              })
            }
          })
        }
        else if(this.data.activity=='seckill'||this.data.activity=='seckill_list'){
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Seckill_getSeckill',
              user_id:wx.getStorageSync('user_id'),
              id:that.data.goodId
            },
            success: function(res) {
              var endtime=Date.parse(new Date())/1000+res.data.data.activity.end_time
              var productInfo=res.data.data.goods
              productInfo['banner']=JSON.parse(res.data.data.goods.pic_group)
              wx.setStorageSync('thumbimg',productInfo['banner'][0])
               that.setData({
                activityInfo:res.data.data.activity,
                productInfo:productInfo,
                yuanprice:parseFloat(res.data.data.activity.activity_price).toFixed(0)+'.',
                yuanprice2:res.data.data.activity.activity_price.substr(res.data.data.activity.activity_price.length-2,2),
                endtime:endtime
               })
            }
          })
        }
        else{
          this.goods_info(this.data.goodId)
        }
        wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Goods_cartList',
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
              var cartCount=0;
              for (var i = 0; i <res.data.data.length; i++) {
                cartCount=parseInt(cartCount)+parseInt(res.data.data[i]['num'])
              }
              that.setData({
                  cartCount:cartCount
              })
            }
          })
      }
     },
    lifetimes: {
      ready:function(){
          wx.getSystemInfo({
            success: (res)=>{
              this.setData({
                screen_width: res.windowWidth/375,
                screen_height: res.windowHeight
             })
            },
          });
        },
        attached: function () {
          var that=this
          wx.request({
            url: app.globalData.siteroot,
            data: { 
              do:'Video_getVideo'
            },
            success: function(res) {
              that.setData({
                showCustomerService:res.data.site_config.is_show_customer_service,
                customer_service_type:res.data.site_config.customer_service_type
              })
            }
          })
        }
      },
    methods: {
      nowyuyue:function(){
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=services_order_create&orderid='+this.data.productInfo.id
        }) 
      },
      handleClose2:function(){
        wx.navigateBack({
          delta:1, 
          success: function() {
          }
        })
      },
      goods_info:function(good_id){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_getGoodsInfo',
            goods_id:good_id,
            user_id:wx.getStorageSync('user_id')
          },
          success: function(res) {
              var spec_id_group='';
              if(typeof(res.data.data)!='undefined'){
                if(typeof(res.data.data.spec_relate)!='undefined'){
                  var newArray=res.data.data.spec_relate;
                  for (var i = 0; i <newArray.length; i++) {
                    if(spec_id_group=='')
                    {
                      spec_id_group=newArray[i].spec_id+':'+newArray[i].value_id;
                    }
                    else{
                       if(newArray[i].spec_id!=newArray[i-1].spec_id)
                       {
                          spec_id_group=spec_id_group+';'+newArray[i].spec_id+':'+newArray[i].value_id;
                       }
                    }
                  }
                  that.setData({
                    spec_id_group:spec_id_group,
                    attrList:res.data.data.attr_relate
                  })
                }
              }
              var productInfo=res.data.data.goods
              productInfo['banner']=JSON.parse(res.data.data.goods.pic_group)
              wx.setStorageSync('thumbimg',productInfo['banner'][0])
              that.setData({
                productInfo:productInfo,
                computedSaleCount:res.data.data.goods.sales_volume,
                collectionStatus:res.data.data.collection_status,
              })
          }
        })
      },
      wetchat:function(){
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=chat'
        })
      },
      addcart4:function(){     
        var choose=[]; 
            choose['buy_type']='group2';
            choose['buy_page']='detail';
            choose['buy_num']=1;
            choose['goodId']=this.data.goodId;
            this.triggerEvent ('get_goods',choose)
      },
      addcart3:function(){     
        var choose=[]; 
            choose['buy_type']='group';
            choose['buy_page']='detail';
            choose['buy_num']=1;
            choose['goodId']=this.data.goodId;
            this.triggerEvent ('get_goods',choose)
      },
      addcart2:function(){     
        var choose=[];
            choose['buy_type']='integral';
            choose['buy_page']='detail';
            choose['buy_num']=1;
            choose['goodId']=this.data.goodId;
            this.triggerEvent ('get_goods',choose)
      },
      addcart:function(){     
        var choose=[];
            choose['buy_type']='add';
            choose['buy_page']='detail';
            choose['buy_num']=1;
            choose['goodId']=this.data.goodId;
            this.triggerEvent ('get_goods',choose)
      },
      seckillnowbuy:function(){   
        var choose=[];
        choose['buy_type']='seckillnowbuy';
        choose['buy_page']='detail';
        choose['buy_num']=1; 
        choose['activity_id']=this.data.activityInfo.id; 
        choose['goodId']=this.data.activityInfo.id; 
        this.triggerEvent ('get_goods',choose)
      },
      bargainnowbuy:function(){   
        var choose=[];
        choose['buy_type']='nowbuy';
        choose['buy_page']='detail';
        choose['buy_num']=1;
        choose['goodId']=this.data.activityInfo.goods_id;
        this.triggerEvent ('get_goods',choose)
      },
      nowbuy:function(){   
        var choose=[];
        choose['buy_type']='nowbuy';
        choose['buy_page']='detail';
        choose['buy_num']=1;
        choose['goodId']=this.data.goodId;
        this.triggerEvent ('get_goods',choose)
      },
      bargaining:function(){   
        var choose=[];
        choose['buy_type']='bargain';
        choose['buy_page']='detail';
        choose['buy_num']=1;
        choose['goodId']=this.data.goodId;
        this.triggerEvent ('get_goods',choose)
      },
      collection:function(){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Member_userCollectionEdit',
            user_id:wx.getStorageSync('user_id'),
            goods_id:that.data.goodId
          },
          success: function(res) {
            that.setData({
              collectionStatus:res.data.data
            })
            var title='';
            if(res.data.data==1)
            {
              title='添加成功！';
            }else{
              title='取消成功！';
            }
            wx.showToast({
              title:title,
              icon: 'none',
              duration: 1500
            })
          }
        })
      },
      saveShareImg: function () {
        let posters = this.data.posters;
        wx.saveImageToPhotosAlbum({
         filePath: posters,
         success: (res)=>{
           wx.showToast({
             title:'保存成功',
             icon:'success'
           })
           this.setData({
             posters:'',
             shareDialogPosterVisible:false,
             shareDialogPosterMask:false
           })
         },
         fail: (err)=>{
           wx.showToast({
             title: '请授权保存图片',
             icon: 'none',
           });
           this.setData({
             isShare:true
           })
         },
       })
         
       },
      cancelhaibao:function(){
        this.setData({
          shareDialogPosterVisible:false,
          shareDialogPosterMask:false
        })
      },
      bargaininginfo:function(){
        wx.navigateTo({ 
          url: '/yb_shopv2/native/all/index?type=bargain_detail&activity_id='+this.data.goodId+'&orderid='+this.data.activityInfo.launch_id
       })
      },
      sharehaibao:function(){
        wx.showLoading({
          title: '绘制中',
          mask: true,
        });
        let that = this;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Qrcode_getQrCode',
            path:'yb_shopv2/native/all/index?type=good_detail&goods_id='+that.data.productInfo.id
          },
          success: function(res) {
               let market_price=that.data.productInfo.market_price;
               let price=that.data.productInfo.price;
               if(typeof(that.data.activityInfo)!='undefined'){
                 if(that.data.activity=='integral'||that.data.activity=='integral_list'){
                  price=that.data.activityInfo.cashprice+'+'+that.data.activityInfo.integral+'积分'
                 }
                 if(that.data.activity=='group'||that.data.activity=='group_list'){
                  price=that.data.activityInfo.activity_price
                 }
                 if(that.data.activity=='bargain'||that.data.activity=='bargain_list'){
                  price=that.data.activityInfo.bottom_price
                 }
                 if(that.data.activity=='seckill'||that.data.activity=='seckill_list'){
                  price=that.data.activityInfo.activity_price
                 }
               }
               let title =that.data.productInfo.share_title;
               let detailImg =app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+that.data.productInfo.pic;
               let codeImg =res.data.qrurl;
               var user=wx.getStorageSync('userinfo')
               let nickName = user.nick_name
               let headUrl = app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+user.user_headimg;
               Promise.all([
                that.getCode(detailImg),
                that.getCode(codeImg),
                that.getCode(headUrl)
               ]).then(res=>{
                   let sWidth = that.data.screen_width;
                   let sHeight = that.data.screen_height;
                   let code = res[1].tempFilePath;
                   let img = res[0].tempFilePath;
                   let headimg = res[2].tempFilePath;
                   detailCanvas(that,sWidth,sHeight,headimg,img,code,title,market_price,price,nickName,function(res){
                   wx.hideLoading();
                   that.setData({ 
                     posters:res
                   });
                 });
               })
          }
        })
        this.setData({
          shareDialogVisible:false,
          shareDialogMask:false,
          shareDialogPosterVisible:true,
          shareDialogPosterMask:true
        })
      },
      getCode(img){
        return new Promise((resolve,reject)=>{
          wx.downloadFile({
            url: img,
            success: (res) => {
              resolve(res)
            }
          })
        })
      },
      sharefeiend:function(){
        this.setData({
          shareDialogVisible:false,
          shareDialogMask:false
        })
      },
      cancelshare:function(){
        this.setData({
          shareDialogVisible:false,
          shareDialogMask:false
        })
      },
      sharegoods:function(){
        this.setData({
          shareDialogMask:true,
          shareDialogVisible:true
       })
       var share=[];
       share['desc']=this.data.productInfo['promote_word'],
       share['title']=this.data.productInfo['goods_name'],
       share['cover']=this.data.productInfo['pic'],
       share['id']=this.data.productInfo['id'],
       this.triggerEvent ('sharefriend',share)
      },
      home:function(){
        wx.reLaunch({
          url: '/yb_shopv2/entry/video-market/index',
        })
      },
      cart:function(){
         wx.navigateTo({
           url: '/yb_shopv2/native/all/index?type=shop_car',
         })
      },
      changetab:function(e){
          this.setData({
            tabCur:e.detail.cur
          })
          if(e.detail.cur==2){
            var that=this
            var commentgoodId=that.data.goodId
            if(this.data.activity=='integral'||this.data.activity=='group'||this.data.activity=='bargain'||this.data.activity=='service'||this.data.activity=='seckill'||this.data.activity=='integral_list'||this.data.activity=='group_list'||this.data.activity=='bargain_list'||this.data.activity=='service_list'||this.data.activity=='seckill_list'){
              commentgoodId=this.data.activityInfo.goods_id
            }
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Goods_getComment',
                user_id:wx.getStorageSync('user_id'),
                goods_id:commentgoodId
              },
              success: function(res) {
                that.setData({
                  commentList:res.data.data
                })
              }
            })
          }
      },
      updatecartCount:function(){
          var that=this
          this.setData({
            cartCount:that.data.cartCount
          })
      }  
    }
});
